package com.nt.train.controller;

import com.nt.train.dto.OrderInfo;
import com.nt.train.service.OrderService;
import com.nt.train.service.OrderStrategyService;
import com.nt.train.service.context.OrderStrategyContext;
import com.nt.train.service.impl.OrderDomesticServiceimpl;
import com.nt.train.service.impl.OrderOverseasServiceImpl;
import com.nt.train.service.impl.OrderRebateServiceImpl;
import com.nt.train.utils.SpringUtil;
import com.nt.train.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 外部调用接口
 * @author hlf
 * @version 1.0
 * @date 2022/7/28 8:51
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderStrategyContext orderStrategyContext;

    @Autowired
    private OrderService orderService;

    @PostMapping("/preCreateOrder")
    public ResultVo preCreateOrder(@RequestBody OrderInfo orderInfo){
        OrderStrategyService orderService =null;
        if("Domestic".equals(orderInfo.getPlatFormType())){
            //国内预下单
             orderService  =  new OrderDomesticServiceimpl();
            return  ResultVo.ok(orderService.preCreateOrder(orderInfo));
        }
        if("Overseas".equals(orderInfo.getPlatFormType())){
            //海外预下单
             orderService  =  new OrderOverseasServiceImpl();
            return  ResultVo.ok(orderService.preCreateOrder(orderInfo));
        }
        if("Rebate".equals(orderInfo.getPlatFormType())){
             //国内特殊回扣
             orderService  =  new OrderRebateServiceImpl();
            return  ResultVo.ok(orderService.preCreateOrder(orderInfo));
        }
        return null;
    }

    @PostMapping("/preCreateOrder2")
    public ResultVo preCreateOrder2(@RequestBody OrderInfo orderInfo){
        OrderStrategyService orderService =null;
        if("Domestic".equals(orderInfo.getPlatFormType())){
            //国内预下单
            orderService  = SpringUtil.getBean("Domestic",OrderStrategyService.class);
            return  ResultVo.ok(orderService.preCreateOrder(orderInfo));
        }
        if("Overseas".equals(orderInfo.getPlatFormType())){
            //海外预下单
            orderService  = SpringUtil.getBean("Overseas",OrderStrategyService.class);
            return  ResultVo.ok(orderService.preCreateOrder(orderInfo));
        }
        if("Rebate".equals(orderInfo.getPlatFormType())){
            //国内特殊回扣
            orderService  = SpringUtil.getBean("Rebate",OrderStrategyService.class);
            return  ResultVo.ok(orderService.preCreateOrder(orderInfo));
        }
        return null;
    }

    @PostMapping("/testStrategy")
    public ResultVo testStrategy(@RequestBody OrderInfo orderInfo){
        OrderStrategyService orderService= orderStrategyContext.getResource(orderInfo);
        return ResultVo.ok(orderService.preCreateOrder(orderInfo));
    }

    /**
     * 高阶处理
     * @param orderType
     * @return
     */
    @GetMapping("/handler")
    public String handleOrder(Integer orderType) {
        OrderInfo order = new OrderInfo();
        order.setBuyer("小明");
        order.setGoodsName("华为手机");
        order.setPrice(3999.9);
        order.setOrderType(orderType);
        return orderService.handleOrder(order);
    }
}
